﻿using My.Core.Quartz.Model;
using My.Core.Quartz.Server.iServer;
using My.Framework;
using NLog;
using SqlSugar.IOC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Quartz.Logging.OperationName;

namespace My.Core.Quartz.Extension
{
    public class JobTasksExtension
    {
		/// <summary>
		/// 日志接口
		/// </summary>
		private static readonly Logger logger = LogManager.GetCurrentClassLogger();

		public static void AddJobTasks()
        {
            ITaskSchedulerServer _schedulerServer = App.GetRequiredService<ITaskSchedulerServer>();

            //var tasks = _tasksQzService.GetList(m => m.IsStart);
            var tasks = DbScoped.SugarScope.Queryable<SysTasks>().Where(m => m.IsStart).ToList();

            //程序启动后注册所有定时任务
            foreach (var task in tasks)
            {
                var result = _schedulerServer.AddTaskScheduleAsync(task);
                if (result.Result.code == 200)
                {
                    Console.WriteLine($"注册任务[{task.Name}]ID：{task.ID}成功");
					logger.Info($"注册任务[{task.Name}]ID：{task.ID}成功");
				}
            }
        }
    }
}
